<?php



namespace App\Transformer;


use App\Models\Role;
use League\Fractal\TransformerAbstract;

class RoleTransformer extends TransformerAbstract
{

    public function transform(Role $data)
    {
        $res = [
            'id' => $data->id,
            'name' => $data->name,
            'desc' => $data->desc,
            'created_at' => $data->created_at->toDateTimeString()
        ];

        if ($data->relationLoaded('permissions') && $data->permissions) {
            $permissions = [];
            foreach ($data->permissions as $item) {
                array_push($permissions, $item->name);
            }
            $res['permissions'] = $permissions;
        }
        return $res;
    }

}
